<?php
/**
 * NOTICE OF LICENSE
 *
 * This source file is subject to the  Academic Free License (AFL 3.0)
 * that is available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to support@zprofess.com so we can send you a copy immediately.
 *
 * @category   	Prestashop module
 * @package		Help Desk
 * @version 	1.0
 * @author 		Kurosaki <kurosaki@zprofess.com>
 * @copyright   Copyright (c) 2012 ZPROFESS. (http://zprofess.com)
 * @license    	http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */
/**
 * @see ZhelpdeskConfig.php
 */
require (_PS_MODULE_DIR_."zhelpdesk/classes/ZhelpdeskConfig.php");

class zhelpdesk extends ZhelpdeskConfig 
{
	/**
	 * File install
	 * @var string
	 */
	const INSTALL_SQL_FILE = 'sql/install.sql';
	
	/**
	 * File Uninstall
	 * @var string
	 */
	const UNINSTALL_SQL_FILE = 'sql/uninstall.sql';
	
	/**
	 * 
	 * Sole constructor
	 */
	public function __construct()
	{
		$this->version='1.0.0';
		$this->author = 'zprofess.com';		
		$this->name='zhelpdesk';	
		$this->tab = 'other';
		parent::__construct();
		$this->displayName = $this->l('Help Desk');
		$this->description = $this->l('This module help you more efficiently handle service requests, and automate all your processes.');
        $this->confirmUninstall = $this->l('Uninstall Help Desk?');
	}
	
	/**
     * Intall module. 
     * Creates module tables, add tabs and adds configurations to configuration table.
     * 
     * @return boolean true if this module installs successfully, false otherwise
     */
    public function install() 
    {
        if (!$this->initConfiguration())
        	return false;
        
		if ( !$this->_addAdminTab(array('parentClassName' 	=> '',
									    'childClassName' 	=> 'ZAdminHelpDesk',
									    'childTabName' 		=> 'Help Desk'))
		  OR !$this->_addAdminTab(array('parentClassName' 	=> 'ZAdminHelpDesk',
									    'childClassName' 	=> 'ZadminTicket',
									    'childTabName' 		=> 'Ticket '))
		  OR !$this->_addAdminTab(array('parentClassName' 	=> 'ZAdminHelpDesk',
									    'childClassName' 	=> 'Zadmindepartment',
									    'childTabName' 		=> 'Departments '))
		  OR !$this->_addAdminTab(array('parentClassName' 	=> 'ZAdminHelpDesk',
									    'childClassName' 	=> 'ZadminStatus',
									    'childTabName' 		=> 'Status'))
		  OR !$this->_addAdminTab(array('parentClassName' 	=> 'ZAdminHelpDesk',
									    'childClassName' 	=> 'ZadminPriority',
									    'childTabName' 		=> 'Priority'))
		  OR !$this->_addAdminTab(array('parentClassName' 	=> 'ZAdminHelpDesk',
									    'childClassName' 	=> 'ZadminRating',
									    'childTabName' 		=> 'Rating'))
		  OR !$this->_addAdminTab(array('parentClassName' 	=> 'ZAdminHelpDesk',
									    'childClassName' 	=> 'ZadminStatistic',
									    'childTabName' 		=> 'Statistic')))
			return false;	
			
		if (!$this->_sql(self::INSTALL_SQL_FILE))
			return false;
			
		if (!parent::install())
			return false;
			
		if ( !$this->registerHook('myAccountBlock') 
		  || !$this->registerHook('leftColumn') 
		  || !$this->registerHook('rightColumn'))
            return false;
        return true;
    }

    /**
     * Uninstall module.
     * Drops module tables from the DB, remove tabs and removes configurations from the configuration table.
     * 
     * @return boolean true if this module uninstalls successfully, false otherwise
     */
	public function uninstall()
	{
		if (!$this->_sql(self::UNINSTALL_SQL_FILE))
			return false;		
		if ( !$this->_removeAdminTab('ZAdminHelpDesk')
		  || !$this->_removeAdminTab('ZadminTicket')
		  || !$this->_removeAdminTab('Zadmindepartment')
		  || !$this->_removeAdminTab('ZadminStatus')
		  || !$this->_removeAdminTab('ZadminPriority')
		  || !$this->_removeAdminTab('ZadminRating')
		  || !$this->_removeAdminTab('ZadminStatistic'))
			return false;
		if (!parent::uninstall())
			return false;
		if (!$this->destroyConfiguration())
			return false;
		return true;
	}
	
	/**
	 * Add Helpdesk link to myaccount block
	 */
	public function hookMyAccountBlock(){
		if (Configuration::get('ZHD_BLOCK_MY_ACCOUNT') == 1)
        	return $this->display(__FILE__, 'themes/block-ticket-link.tpl');
    }
    
    /**
     * Add Helpdesk block to left column
     */
	public function hookLeftColumn()
	{
		global $smarty, $cookie;
		if (Configuration::get('ZHD_COLUMN')==1) {
			$linkPost = Tools::getShopDomain(true).__PS_BASE_URI__.'modules/zhelpdesk/zhelpdesk-post-ticket.php';
	    	$linkTicket = Tools::getShopDomain(true).__PS_BASE_URI__.'modules/zhelpdesk/zhelpdesk-ticket.php';
	    	$id_customer = (!$id_customer = (int)$cookie->id_customer) ? false : (int)$cookie->id_customer;
	    	$smarty->assign(array(
				'linkPost'=> $linkPost,
	    		'linkTicket' => $linkTicket,
	    		'id_customer' => $id_customer
			));
        	return $this->display(__FILE__, 'themes/hook-left.tpl');
		}
    }
    
	/**
     * Add Helpdesk block to right column
     */
	public function hookRightColumn()
	{
		global $smarty, $cookie;
		if (Configuration::get('ZHD_COLUMN')==2) {
			$linkPost = Tools::getShopDomain(true).__PS_BASE_URI__.'modules/zhelpdesk/zhelpdesk-post-ticket.php';
	    	$linkTicket = Tools::getShopDomain(true).__PS_BASE_URI__.'modules/zhelpdesk/zhelpdesk-ticket.php';
	    	$id_customer = (!$id_customer = (int)$cookie->id_customer) ? false : (int)$cookie->id_customer;
	    	$smarty->assign(array(
				'linkPost'=> $linkPost,
	    		'linkTicket' => $linkTicket,
	    		'id_customer' => $id_customer
			));
        	return $this->display(__FILE__, 'themes/hook-left.tpl');
		}
    }
}
